package com.hhf.cloud.controller;

import com.hhf.cloud.entities.Pay;
import com.hhf.cloud.entities.PayDTO;
import com.hhf.cloud.resp.ResultData;
import com.hhf.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Daniel-黄海峰
 * @Description
 * @create 2024/2/27 9:10
 */
@RestController
@Tag(name = "支付微服务模块",description = "支付CRUD")
@Slf4j
public class PayController {

    @Resource
    private PayService payService;

    @PostMapping(value = "/pay/add")
    @Operation(summary = "新增",description = "新增支付流水方法,json串做参数")
    public ResultData<String> addPay(@RequestBody Pay pay){
        System.out.println(pay.toString());
        int i = payService.add(pay);
        return ResultData.success("成功插入记录，返回值："+i);
    }

    @DeleteMapping(value = "/pay/del/{id}")
    @Operation(summary = "删除",description = "删除支付流水方法")
    public ResultData<Integer> detelePay(@PathVariable("id") Integer id){
        int i = payService.delete(id);
        return  ResultData.success(i);
    }

    @PutMapping(value = "/pay/update")
    @Operation(summary = "修改",description = "修改支付流水方法")
    private ResultData<String> updatePay(@RequestBody PayDTO payDTO){
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO,pay);
        int i = payService.update(pay);
        return ResultData.success("成功修改记录，返回值："+i);
    }

    @GetMapping(value = "/pay/get/{id}")
    @Operation(summary = "按照ID查流水",description = "查询支付流水方法")
    public ResultData<Pay> getById(@PathVariable("id") Integer id){
        Pay pay = payService.getById(id);
        //暂停62秒，测试出feign的默认超时时间
        try { TimeUnit.SECONDS.sleep(62); }catch (Exception e) {e.printStackTrace();}
        return ResultData.success(pay);
    }

    @GetMapping("/pay/all")
    @Operation(summary = "查询支付的所有信息",description = "查询支付的所有信息方法")
    public ResultData<List<Pay>> getAll(){
        List<Pay> all = payService.getAll();
        return ResultData.success(all);
    }

    @Value("${server.port}")
    private String port;

    @GetMapping(value = "/pay/get/info")
    public String getInfoByConsul( @Value("${hhfttss.info}") String hhfttss){
        return "hhfInfo: "+hhfttss+"\t"+"port: "+port;
    }
}
